<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Fileclip extends Controller
{


    public function _initialize()
    {
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    public function uploadclip()
    {
        $width = 710;
        $height = 380;
        $dataid = trim(input("request.dataid"));
        $tmpWidth = trim(input("request.width"));
        $tmpHeight = trim(input("request.height"));
        $msgshow = trim(input("request.msgshow"));
        if (!empty($tmpWidth)) {
            $width = $tmpWidth;
        }
        if (!empty($tmpHeight)) {
            $height = $tmpHeight;
        }
        $this->assign('dataid', $dataid);
        $this->assign('height', $height);
        $this->assign('width', $width);
        $this->assign('msgshow', $msgshow);

        return view('uploadclip');
    }

    public function addClipImg()
    {
        $time = time();
        $file = trim(input("request.file"));//auth
        $pathUrl = DS . 'static' . DS . 'uploads' . DS . 'images' . DS . date("Y-m-d");
        $movepath = ROOT_PATH . 'public' . $pathUrl;
        $fileTemp = explode(";", $file);
        $filename = "";
        if ($fileTemp[0] == "data:image/jpeg" || $fileTemp[0] == "data:image/jpg") {
            $filename = $time . ".jpg";
        } else if ($fileTemp[0] == "data:image/png") {
            $filename = $time . ".png";
        }


        if (!file_exists($movepath)) {
            mkdir($movepath, 0777, true);

        }

        $path = $movepath . DS . $filename;

        $imgDataTmp = explode(",", $file);
        $file = base64_decode($imgDataTmp[1]);

        file_put_contents($path, $file);


        $iData = array(
            'pic' => str_replace("\\", "/", $pathUrl . DS . $filename),
        );

        if ($iData['pic']) {
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = $iData;
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            $return['data'] = $iData;
        }
        return json($return);

    }

}